package 多线程案例;
//CountDownLatch——模拟下载任务
import java.util.Random;
import java.util.concurrent.CountDownLatch;

public class Demo_13 {
    public static void main(String[] args) throws InterruptedException {

        CountDownLatch countDownLatch =new CountDownLatch(10);

        for (int i = 0; i < 10; i++) {
            int id = i;
            Thread t = new Thread(()->{
                System.out.println("任务"+id+"开始下载");
                try {
                    Random random = new Random();
                    int time = random.nextInt(6)*1000;
                    Thread.sleep(time);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("任务"+id+"结束下载");
                //告知任务完成
                countDownLatch.countDown();
            });
            t.start();
        }
        //等所有的任务都完成
        countDownLatch.await();
        System.out.println("任务都完成了");
    }
}
